package org.huangrui.javase.collectionMap.collection;

import org.huangrui.entities.Employee;
import org.junit.Test;

import java.util.Comparator;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.TreeSet;

/**
 * @Author hr
 * @Create 2025-11-07 16:07
 **/
public class T04Set {
    @Test
    public void test1(){
        HashSet<String> set = new HashSet<>();
        set.add("atguigu");
        set.add("java");
        set.add("hello");
        set.add("hello");
        set.add("hello");
        System.out.println(set);
    }

    @Test
    public void test2(){
        LinkedHashSet<String> set = new LinkedHashSet<>();
        set.add("atguigu");
        set.add("java");
        set.add("hello");
        set.add("hello");
        set.add("hello");
        System.out.println(set);
    }

    @Test
    public void test3(){
        TreeSet<String> set = new TreeSet<>();
        set.add("atguigu");
        set.add("java");
        set.add("hello");
        set.add("hello");
        set.add("hello");
        System.out.println(set);
    }

    @Test
    public void test4()throws Exception{
        TreeSet<Employee> set = new TreeSet<>();
        set.add(new Employee(2,"谷粒",15000));
        set.add(new Employee(1,"谷姐",16000));
        set.add(new Employee(3,"谷子",13000));

        for (Employee e : set) {
            System.out.println(e);
        }
    }

    @Test
    public void test5()throws Exception{
        Comparator<Employee> c = new Comparator<Employee>() {
            @Override
            public int compare(Employee o1, Employee o2) {
                return Double.compare(o1.getSalary(),o2.getSalary());
            }
        };
        TreeSet<Employee> set = new TreeSet<>(c);
        set.add(new Employee(2,"谷粒",15000));
        set.add(new Employee(1,"谷姐",16000));
        set.add(new Employee(3,"谷子",13000));

        for (Employee e : set) {
            System.out.println(e);
        }
    }

    @Test
    public void test6()throws Exception{
        TreeSet<Employee> set = new TreeSet<>(Comparator.comparingDouble(Employee::getSalary));
        set.add(new Employee(2,"谷粒",15000));
        set.add(new Employee(1,"谷姐",16000));
        set.add(new Employee(3,"谷子",13000));

        for (Employee e : set) {
            System.out.println(e);
        }
    }
}
